package de.hsfulda.et.problemloesen.ws1112.grp2.knapsack;

import java.util.Random;
import java.util.Vector;

public class Randomization implements Optimizer{
	
	public Knapsack optimize(int i, Vector<KnapsackItem> items,
			Vector<Long> boundaryVector) {
		
		Long numberResultItems = 0L;
		int numberResultItemsInt = 0;
		Random rand = new Random();
		
		Long[] resultItemsList;  
		
		Knapsack ks = new Knapsack(items.size());
		
		Long sum = 0L;
		
		int randomnumber;
		
		boolean local = true;
		
		Long[] liste = new Long[items.size()];
		Long[] cloneliste1 = new Long[items.size()];
		Long[] cloneliste2 = new Long[items.size()];
		
		for(int j = 0; j < items.size(); j++){
			KnapsackItem o = items.get(j);
			liste[j] = o.getWeight();
		}
		cloneliste1 = liste.clone();
		cloneliste2 = liste.clone();
		
		int n=0;
		
		while(local == true){
			
			//System.out.println(n++);
			
			cloneliste1 = liste.clone();
			
		//	numberResultItemsInt = rand.nextInt(items.size());
			numberResultItemsInt = 4;
			resultItemsList = new Long[numberResultItemsInt];
			for(int j = 0 ; j < numberResultItemsInt ; j++){
				
				randomnumber = rand.nextInt(cloneliste1.length); 
				
				while(cloneliste1[randomnumber] == 0L){
					randomnumber = rand.nextInt(cloneliste1.length);
				}
				
				sum = sum + cloneliste1[randomnumber];	
				
				resultItemsList[j] = cloneliste1[randomnumber];
				
				cloneliste1[randomnumber] = 0L;
				
				if(sum >= boundaryVector.get(0)){
					break;
				}
			}
			if(sum == boundaryVector.get(0)){
				local = false;
			}
			sum = 0L;
		}
		
		
		
		numberResultItems = (long) numberResultItemsInt;
		
		for(int j=0;j<items.size();j++){
		    KnapsackItem o = items.get(j);
		    resultItemsList 
		    ks.packIn(o, j);
		}
		
	return ks;	
	}
	
	
}
